﻿using ShareFrame.Entity.Base_BasicData;
using ShareFrame.Entity.DTO;
using ShareFrame.Util;
using ShareFrame.Util.Helper;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using ShareFrame.IBusiness.Base_BasicData;

namespace ShareFrame.Business.Base_BasicData
{
    /// <summary>
    /// 省市区表接口实现类
    /// </summary>
    public class Base_RegionBusiness : BaseBusiness<Base_Region>, IBase_RegionBusiness, ITransientDependency
    {
        public Base_RegionBusiness(IDbAccessor db)
            : base(db)
        {
        }

        #region 外部接口
        /// <summary>
        /// 根据条件获取分页结果集
        /// </summary>
        /// <param name="input">传入条件参数</param>
        /// <returns></returns>
        public async Task<PageResult<Base_Region>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<Base_Region>();
            var search = input.Search;

            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<Base_Region, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }

            return await q.Where(where).GetPageResultAsync(input);
        }

        /// <summary>
        /// 根据PID获取下属子集
        /// </summary>
        /// <param name="basedbOptions">数据库连接字符串</param>
        /// <param name="pId">父级编号</param>
        /// <returns></returns>
        public async Task<PageResult<Base_Region>> GetDataListAsync(BaseDbOption basedbOptions, string pId)
        {
            var dapper = new DapperHelper<Base_Region>(basedbOptions.ConnectionString);

            string sql = string.Format("SELECT * FROM Base_Region WHERE Pid = @Pid", pId);

            return await dapper.QueryAsync(sql, new { Pid = pId });
        }

        /// <summary>
        /// 根据传入Id获取对应实体信息
        /// </summary>
        /// <param name="id">实体唯一编号</param>
        /// <returns></returns>
        public async Task<Base_Region> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        /// <summary>
        /// 基础添加方法
        /// </summary>
        /// <param name="data">要添加的实体信息</param>
        /// <returns></returns>
        public async Task AddDataAsync(Base_Region data)
        {
            await InsertAsync(data);
        }

        /// <summary>
        /// 基础更新方法
        /// </summary>
        /// <param name="data">要更新的实体信息</param>
        /// <returns></returns>
        public async Task UpdateDataAsync(Base_Region data)
        {
            await UpdateAsync(data);
        }

        /// <summary>
        /// 基础删除方法
        /// </summary>
        /// <param name="ids">传入条件参数</param>
        /// <returns></returns>
        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        #endregion

        #region 私有成员

        #endregion
    }
}